﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.LabManage
{
    public partial class PreViewRule : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("GZZD");
            if (UserType == "student" || UserType == "teacher")
            {
                modifybtn.Visible = false;
                DeleteBtn.Visible = false;
            }
            if (Request.QueryString["ID"] != null)
            {
                if (!IsPostBack)
                {
                    int ID;
                    string strID = Request.QueryString["ID"].ToString();
                    if (int.TryParse(strID, out ID))
                    {
                        IDHF.Value = ID.ToString();
                        ExtendBLL.Rule rule = new ExtendBLL.Rule();
                        DataSet ds = new DataSet();

                        ds = rule.GetList("[ID]=" + ID);
                        CodeNumLbl.Text = ds.Tables[0].Rows[0]["Rule_CodeNum"].ToString();
                        TitleLbl.Text = ds.Tables[0].Rows[0]["Rule_Title"].ToString();
                        SubTitleLbl.Text = ds.Tables[0].Rows[0]["Rule_SubTitle"].ToString();
                        KeyLbl.Text = ds.Tables[0].Rows[0]["Rule_KeyWord"].ToString();
                        OutputUnitLbl.Text = ds.Tables[0].Rows[0]["Rule_OutputUnit"].ToString();
                        ResourceContentLbl.Text = ds.Tables[0].Rows[0]["Rule_Content"].ToString();//内容
                        ModifyDateLbl.Text = string.Format("{0:d}", ds.Tables[0].Rows[0]["Rule_Date"].ToString());//日期
                        List<LabMS.Model.Dictionary> dic = new List<LabMS.Model.Dictionary>();
                        //绑定数据字典
                        dic = DictionaryDropdownlist("118");
                        RuleKindDDL.DataSource = dic;
                        RuleKindDDL.DataBind();
                        RuleKindDDL.SelectedValue = ds.Tables[0].Rows[0]["Rule_Type"].ToString();

                        //绑定录入单位
                        LabDropdowlist();
                        InputUnitDDL.SelectedValue = ds.Tables[0].Rows[0]["Rule_InputUnit"].ToString();

                        //获取文件列表
                        string strHTML = "";
                        if (ds.Tables[0].Rows[0]["AttachmentID"].ToString() != "")
                        {
                            LabMS.BLL.AttachmentItem attIte = new LabMS.BLL.AttachmentItem();
                            List<LabMS.Model.AttachmentItem> attIteModelList = new List<LabMS.Model.AttachmentItem>();

                            attIteModelList = attIte.GetModelList("Attach_ID=" + int.Parse(ds.Tables[0].Rows[0]["AttachmentID"].ToString()));
                            for (int i = 0; i < attIteModelList.Count; i++)
                            {
                                strHTML += "<a onclick='downloadfile(" + attIteModelList[i].ID + ")'>";
                                strHTML += attIteModelList[i].Item_Name;
                                strHTML += "</a>";
                                strHTML += "<br/>";
                            }
                            filehtml.InnerHtml = strHTML;
                        }
                    }
                    else
                    {
                        ERRLbl.Text = "系统异常，ID值不合法！";
                        ERRLbl.Visible = true;
                        modifybtn.Visible = false;
                        DeleteBtn.Visible = false;
                    }
                }
            }
            else
            {
                ERRLbl.Text = "系统异常，ID值不合法！";
                ERRLbl.Visible = true;
                modifybtn.Visible = false;
                DeleteBtn.Visible = false;
            }
        }

        //绑定数据字典 返回List
        protected List<LabMS.Model.Dictionary> DictionaryDropdownlist(string code)
        {
            string strwhere = "DClass='" + code + "'";
            LabMS.BLL.Dictionary dic = new LabMS.BLL.Dictionary();

            List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
            list = dic.GetModelList(strwhere);
            return list;
        }

        //绑定所属实验室
        protected void LabDropdowlist()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();

            ParLab = lab.GetModelList("Lab_ParentID is null");

            InputUnitDDL.DataSource = ParLab;
            InputUnitDDL.DataBind();
            ListItem item = new ListItem("学校", "0");
            InputUnitDDL.Items.Insert(0, item);
        }

        //删除
        protected void Delete_OnClick(object sender, EventArgs e)
        {
            ExtendBLL.Rule rule = new ExtendBLL.Rule();
            try
            {
                rule.Delete(int.Parse(IDHF.Value));
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('删除成功!');refreshParent();wincolse();</script>");
            }
            catch
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('删除失败!');refreshParent();wincolse();</script>");
            }

        }
    }
}
